using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNhaSach.BUS.DTO;
using QLNhaSach.DAO;
using QLNhaSach.GUI.NhanVienQuanLySach;
using QLNhaSach.GUI.NhanVienAdmin;
using QLNhaSach.GUI.NhanVienBanSach;
using QLNhaSach.GUI.NhanVienQuanLy;
using QLNhaSach.GUI.NhanVienKeToan;

namespace QLNhaSach.GUI
{
    public partial class DangNhap : Form
    {
        public int manv;
        public DangNhap()
        {
            InitializeComponent();
        }


        private void DangNhap_Load(object sender, EventArgs e)
        {

        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (txtUsername .Equals("") || txtPassword.Text.Equals(""))
            {
                MessageBox.Show("Ban hay nhap username va password", "Loi dang nhap");
                txtUsername.Focus();

            }
            else if (txtUsername.Text.Equals(""))
            {
                MessageBox.Show("Ban hay nhap username ", "Loi dang nhap");
                txtUsername.Focus();
            }
            else if (txtPassword.Text.Equals(""))
            {
                MessageBox.Show("Ban hay nhap password ", "Loi dang nhap");
                txtPassword.Focus();
            }
            else
            {
                NhanVienDTO nv = new NhanVienDTO();
                nv.UserName = txtUsername.Text;
                nv.PassWord = txtPassword.Text;
                DataTable dt = NhanVienDAO.DangNhap(nv);
                if (dt.Rows.Count > 0) // khuc nay chua dg nha
                {
                    this.Hide();
                    nv = NhanVienBUS.DangNhap(nv);
                    manv = nv.MaNV;
                    // 1 - Admin, 2 - QuanLy, 3 - QuanLySach, 4 - KeToan, 5 - NVBanSach
                    if (nv.TinhTrang == 0)
                    {
                        MessageBox.Show("Tai khoan da bi khoa");
                        DangNhap frm = new DangNhap();
                        frm.Show();
                    }
                    else if(nv.PhanQuyen == 1)
                    {
                        Admin fmAdmin = new Admin();
                        fmAdmin.manvadmin = manv;
                        fmAdmin.Show();
                    }
                    else if (nv.PhanQuyen == 2)
                    {
                        NVQuanLy fmNVQuanLy = new NVQuanLy();
                        fmNVQuanLy.manvql = manv;
                        fmNVQuanLy.Show();

                    }
                    else if (nv.PhanQuyen == 3)
                    {
                        NVQLSach fmNVQLSach = new NVQLSach();
                        fmNVQLSach.manvqlsach = manv;
                        fmNVQLSach.Show();
                       
                    }
                    else if (nv.PhanQuyen == 4)
                    {
                        NVKeToan  fmNVKeToan = new NVKeToan();
                        fmNVKeToan.maketoan = manv;
                        fmNVKeToan.Show();
                    }
                    else
                    {
                        NVBanSach fmNVBanSach = new NVBanSach();
                        fmNVBanSach.manvbansach = manv;
                        fmNVBanSach.Show();
                        
                    }

                }
                else
                {
                    MessageBox.Show("username va password khong khop", "Loi dang nhap");
                }
            }
        }

        private void DangNhap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnDangNhap_Click(sender, e);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("QuanLyNhaSach.chm");
            Help.ShowHelp(this,"QuanLyNhaSach.chm");
        }
    }
}